package com.blogManager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.blogManager.entity.PageResult;
import com.blogManager.mapper.TbBlogtypeMapper;
import com.blogManager.pojo.TbBlogtype;
import com.blogManager.pojo.TbBlogtypeExample;
import com.blogManager.pojo.TbBlogtypeExample.Criteria;
import com.blogManager.service.BlogTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class BlogTypeServiceImpl implements BlogTypeService {

	@Autowired
	private TbBlogtypeMapper blogTypeMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	public PageResult getBlogTypeList(int page, int rows) {

		PageHelper.startPage(page, rows);
		List<TbBlogtype> blogTypeList = blogTypeMapper.selectByExample(null);
		PageInfo<TbBlogtype> pageInfo = new PageInfo<TbBlogtype>(blogTypeList);
		// 封装页面包装对象
		PageResult pageResult = new PageResult();
		pageResult.setCurrentPage(pageInfo.getPageNum());
		pageResult.setTotal((int) pageInfo.getTotal());
		pageResult.setList(blogTypeList);
		pageResult.setPageNum(pageInfo.getPages());
		return pageResult;
	}

	public void add(TbBlogtype blogType) {
		redisTemplate.delete("blogTypeList");//删除缓存
		blogTypeMapper.insert(blogType);
	}

	public TbBlogtype getBlogTypeById(int typeId) {
		return blogTypeMapper.selectByPrimaryKey(typeId);
	}

	public void update(TbBlogtype blogType) {
		redisTemplate.delete("blogTypeList");//删除缓存
		blogTypeMapper.updateByPrimaryKey(blogType);
	}

	public void delete(int[] ids) {
		redisTemplate.delete("blogTypeList");//删除缓存
		for (int id : ids) {
			blogTypeMapper.deleteByPrimaryKey(id);
		}
	}

	public PageResult search(int page, int rows, TbBlogtype blogtype) {

		PageHelper.startPage(page, rows);
		TbBlogtypeExample example = new TbBlogtypeExample();
		Criteria criteria = example.createCriteria();
		if (blogtype != null) {
			if (blogtype.getTypename() != null && blogtype.getTypename().length() > 0) {
				criteria.andTypenameLike("%" + blogtype.getTypename() + "%");
			}
		}
		List<TbBlogtype> blogTypeList = blogTypeMapper.selectByExample(example);
		PageInfo<TbBlogtype> pageInfo = new PageInfo<TbBlogtype>(blogTypeList);
		// 封装页面包装对象
		PageResult pageResult = new PageResult();
		pageResult.setCurrentPage(pageInfo.getPageNum());
		pageResult.setTotal((int) pageInfo.getTotal());
		pageResult.setList(blogTypeList);
		pageResult.setPageNum(pageInfo.getPages());
		return pageResult;

	}

	public List<TbBlogtype> findAll() {
		return blogTypeMapper.selectByExample(null);
	}

}
